Public Class BasicCurrencyConversor
    Enum ECurrenciesInCalculation
        Currency1
        Currency2
    End Enum
    Public Currencies As Dictionary(Of Currency.ECurrencySymbol, Currency)
    Public CurrencyPairs As Dictionary(Of CurrencyPair.ECurrencyPairSymbol, CurrencyPair)
#Region "Properties"


    Dim mLastChangedCurrency As ECurrenciesInCalculation
    Public Property LastChangedCurrency() As ECurrenciesInCalculation
        Get
            Return mLastChangedCurrency
        End Get
        Set(ByVal value As ECurrenciesInCalculation)
            mLastChangedCurrency = value
        End Set
    End Property
    Public Property Currency1Amount() As Double
        Get
            Return Val(Me.TxtAmountCurrency1.Text)
        End Get
        Set(ByVal value As Double)
            SetValueInTextbox(value, Me.TxtAmountCurrency1)
        End Set
    End Property
    Public Property Currency2Amount() As Double
        Get
            Return Val(Me.TxtAmountCurrency2.Text)
        End Get
        Set(ByVal value As Double)
            SetValueInTextbox(value, Me.TxtAmountCurrency2)
        End Set
    End Property
    Private Sub SetValueInTextbox(ByVal value As Double, ByVal txt As TextBox)
        RemoveHandler txt.TextChanged, AddressOf TxtAmountCurrency_TextChanged
        txt.Text = value.ToString.Replace(",", ".")
        AddHandler txt.TextChanged, AddressOf TxtAmountCurrency_TextChanged
    End Sub
    Private Function ObtainValidSelectedCurrency(ByVal cmb As ComboBox) As String
        If cmb.SelectedItem IsNot Nothing Then
            Dim SelectedItem As String = cmb.SelectedItem.ToString
            If Currency.IsValidCurrencyString(SelectedItem) Then
                Return SelectedItem
            Else
                Return ""
            End If
        Else
            Return ""
        End If
    End Function
    Private Sub SetSelectedCurrency(ByVal cmb As ComboBox, ByVal currencyString As String)
        If Currency.IsValidCurrencyString(currencyString) AndAlso cmb.Items.Contains(currencyString) Then
            cmb.SelectedItem = currencyString
        End If
    End Sub
    Public Property Currency1() As String
        Get
            Return ObtainValidSelectedCurrency(Me.CmbCurrency1)
        End Get
        Set(ByVal value As String)
            SetSelectedCurrency(Me.CmbCurrency1, value)
        End Set
    End Property
    Public Property Currency2() As String
        Get
            Return ObtainValidSelectedCurrency(Me.CmbCurrency2)
        End Get
        Set(ByVal value As String)
            SetSelectedCurrency(Me.CmbCurrency2, value)
        End Set
    End Property
#End Region
    Public Sub LoadCurrencies()
        Me.CmbCurrency1.Items.Clear()
        Me.CmbCurrency2.Items.Clear()

        If Currencies Is Nothing Then Exit Sub

        CmbCurrency1.BeginUpdate()
        CmbCurrency2.BeginUpdate()

        For Each CPair As KeyValuePair(Of Currency.ECurrencySymbol, Currency) In Currencies
            CmbCurrency1.Items.Add(CPair.Key.ToString)
            CmbCurrency2.Items.Add(CPair.Key.ToString)
        Next
        Me.Currency1 = "USD"
        Me.Currency2 = "EUR"
        CmbCurrency1.EndUpdate()
        CmbCurrency2.EndUpdate()
    End Sub
    Public Sub CalculateConversion()

        If Currency1 = "" Or Currency2 = "" Then Exit Sub
        Dim Currency1Obj As Currency = Currency.GetCurrencyFromString(Currency1, Currencies)
        Dim Currency2Obj As Currency = Currency.GetCurrencyFromString(Currency2, Currencies)

        Try
            Select Case LastChangedCurrency 'o sea que se calcula es el otro
                Case ECurrenciesInCalculation.Currency1
                    Me.Currency2Amount = AmountOfMoney.Convert(New AmountOfMoney(Me.Currency1Amount, Currency1Obj), Currency2Obj, CurrencyPairs, Currencies).Amount
                Case ECurrenciesInCalculation.Currency2
                    Me.Currency1Amount = AmountOfMoney.Convert(New AmountOfMoney(Me.Currency2Amount, Currency2Obj), Currency1Obj, CurrencyPairs, Currencies).Amount
            End Select
        Catch ex As Exception
            MsgBox("An error ocurred:" & vbCrLf & ex.Message)
        End Try

    End Sub

    Private Sub CmbCurrency_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbCurrency1.SelectedIndexChanged, CmbCurrency2.SelectedIndexChanged
        CalculateConversion()
    End Sub
    Private Sub TxtAmountCurrency_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtAmountCurrency1.TextChanged, TxtAmountCurrency2.TextChanged

        If sender Is Me.TxtAmountCurrency2 Then
            LastChangedCurrency = ECurrenciesInCalculation.Currency2
        ElseIf sender Is Me.TxtAmountCurrency1 Then
            LastChangedCurrency = ECurrenciesInCalculation.Currency1
        End If
        CalculateConversion()
    End Sub

End Class
